package com.example.greentrack;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class LoginActivity extends AppCompatActivity {

    // 模拟的合法用户数据（实际项目应使用数据库或网络验证）
    private static final String VALID_USERNAME = "mao";
    private static final String VALID_PASSWORD = "green2025";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        EditText etUsername = findViewById(R.id.et_username);
        EditText etPassword = findViewById(R.id.et_password);
        Button btnLogin = findViewById(R.id.btn_login);

        btnLogin.setOnClickListener(v -> {
            String username = etUsername.getText().toString().trim();
            String password = etPassword.getText().toString().trim();

            if (username.isEmpty() || password.isEmpty()) {
                showToast("账号和密码不能为空");
                return;
            }

            // 实际项目这里应该替换为网络请求或数据库验证
            if (VALID_USERNAME.equals(username) && VALID_PASSWORD.equals(password)) {
                startActivity(new Intent(this, MainActivity.class));
                finish();
            } else {
                showToast("账号或密码错误");
                etPassword.setText(""); // 清空密码框
            }
        });
    }

    private void showToast(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }
}